<?php

function getAllResourcesAvailable()
{
	$return_string = "";
	$dbh = connectDB();
	$query = "SELECT qr_code FROM QR_Code WHERE is_active=1 AND qr_code NOT IN (SELECT DISTINCT qr_code FROM Resource_Designation WHERE has_been_checked_in=0)";
	$stmt = $dbh->prepare($query);
	$stmt->execute();

	$qr_codes_available = $stmt->fetchAll(PDO::FETCH_ASSOC);

	$equipment;
	$rooms;
	foreach( $qr_codes_available as $qr )
	{
		$equip_query = "SELECT * FROM Equipment WHERE qr_code=:qr";
		$rooms_query = "SELECT * FROM Room WHERE qr_code=:qr";

		$stmt = $dbh->prepare($equip_query);
		$stmt->bindParam(':qr',$qr['qr_code']);
		$stmt->execute();
		if( $stmt->rowCount() == 1 )
			$equipment[] = $stmt->fetch(PDO::FETCH_ASSOC);

		$stmt = $dbh->prepare($rooms_query);
		$stmt->bindParam(':qr',$qr['qr_code']);
		$stmt->execute();
		if( $stmt->rowCount() == 1 )
			$rooms[] = $stmt->fetch(PDO::FETCH_ASSOC);
	}

	//PRINT EQUIPMENT CHECKED OUT TABLE 
	$return_string .="<h2>Equipment Currently Checked Out</h2>";
	$return_string .="<table border=1>".
		"<tr><th>QR Code</th>".
		"<th>Equipment Name</th>".
		"<th>Serial Number</th>".
		"<th>Current Condition</th></tr>";
	
	foreach( $equipment as $e )
	{
		$return_string .="<tr><td>".$e['qr_code']."</td>".
			"<td>".$e['equipment_name']."</td>".
			"<td>".$e['serial_number']."</td>".
			"<td>".$e['current_condition']."</td></tr>";
	}
	$return_string .="</table>";

	//PRINT ROOMS CHECKED OUT TABLE
	$return_string .="<h2>Rooms Currently Checked Out</h2>";
	$return_string .="<table border=1>".
		"<tr><th>QR Code</th>".
		"<th>Building</th>".
		"<th>Floor</th>".
		"<th>Room Number</th>".
		"<th>Room Type</th></tr>";
	foreach( $rooms as $r )
	{
		$return_string .="<tr><td>".$r['qr_code']."</td>".
			"<td>".$r['building']."</td>".
			"<td>".$r['floor']."</td>".
			"<td>".$r['room_number']."</td>".
			"<td>".$r['room_type']."</td>"."</tr>";
	}
	$return_string .="</table>";

	return $return_string;
}
?>
